﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using TopologySearch;

namespace ModelChecking.PerformanceAnalysis.ShapeCoeffiecientOfBuilding
{
	internal class StandardSegmentMaker
	{
		public List<StandardSegmentStruct> StandardLevelSegmentList { get; set; }

		public StandardSegmentMaker(DocumentResultPacker documentResPacker)
		{
			this.m_DocumentResPacker = documentResPacker;
			this.m_LevelSegmentList = new List<LevelSegment>();
			this.StandardLevelSegmentList = new List<StandardSegmentStruct>();
		}

		private bool LevelSegmentListInit()
		{
			bool flag = true;
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			double num4 = 0.0;
			Dictionary<double, List<Floor>> floorList = this.m_DocumentResPacker.FloorList;
			if (floorList.Count <= 1)
			{
				return false;
			}
			if (floorList.Count == 2)
			{
				LevelSegment item = new LevelSegment(floorList.ElementAt(0).Key, floorList.ElementAt(1).Key);
				this.m_LevelSegmentList.Add(item);
			}
			for (int i = 0; i < floorList.Count - 1; i++)
			{
				if (flag)
				{
					num = floorList.ElementAt(0).Key;
					num3 = this.GetFloorsArea(floorList.ElementAt(0).Value);
					flag = false;
				}
				num2 = floorList.ElementAt(i + 1).Key;
				num4 = this.GetFloorsArea(floorList.ElementAt(i + 1).Value);
				try
				{
					if (!this.m_DocumentResPacker.WallAndColumnList.ContainsKey(num2))
					{
						num2 = floorList.ElementAt(i + 2).Key;
					}
				}
				catch
				{
				}
				if (num != num2 && Math.Abs(num4 - num3) / num3 > this.m_tolerence)
				{
					LevelSegment item2 = new LevelSegment(num, num2);
					this.m_LevelSegmentList.Add(item2);
					num = num2;
					num3 = num4;
				}
			}
			return true;
		}

		private double GetFloorsArea(List<Floor> floors)
		{
			double num = 0.0;
			foreach (Floor floor in floors)
			{
				Parameter parameter = floor.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED);
				if (parameter != null)
				{
					double num2 = parameter.AsDouble();
					num += num2;
				}
			}
			return num;
		}

		private bool StandardSegmentListInit()
		{
			foreach (LevelSegment levelSegment in this.m_LevelSegmentList)
			{
				try
				{
					List<Pole2D> pole2DList = this.m_DocumentResPacker.WallAndColumnList[levelSegment.ButtomLevel];
					List<List<Curve>> loop = Utility.GetLoop(this.m_DocumentResPacker.Doc, pole2DList);
					StandardSegmentStruct item = new StandardSegmentStruct(levelSegment.ButtomLevel, levelSegment.TopLevel, loop);
					this.StandardLevelSegmentList.Add(item);
				}
				catch (Exception)
				{
				}
			}
			return true;
		}

		public bool StandardSegmentInit()
		{
			this.LevelSegmentListInit();
			this.StandardSegmentListInit();
			return true;
		}

		private double m_tolerence = 0.15;

		private DocumentResultPacker m_DocumentResPacker;

		private List<LevelSegment> m_LevelSegmentList;
	}
}
